import httpx, asyncio
from asyncio import Semaphore
import aiohttp
import ssl
import socket
import time


url = 'https://docs.aiohttp.org/en/stable/'
url2 = 'https://hga050.com'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}

async def spider(i,client, max_concurrent=100):
    semaphore = Semaphore(max_concurrent)  # 设置最大并发数
    async with semaphore:  # 使用信号量限制并发
        async with client.get(url) as response:
            # content = await response.text()  # 获取网页内容
            print('启动{} {}'.format(i,response.status))


async def spider2(i,client):
    content = await client.get(url2)
    print('启动{} {}'.format(i,content.status))

def ssl_connect(host, port):
    ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
    context = ssl_context.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_STREAM), server_hostname=host)
    context.connect((host, port))
    return context

async def aiohttp_test():
    start = time.time()
    conn = aiohttp.TCPConnector(ssl=False)
    async with aiohttp.ClientSession(connector=conn, headers=headers)as client:
        lists=[]
        for i in range(10):
            lists.append(asyncio.create_task(spider2(i,client)))
        await asyncio.gather(*lists)
        end=time.time()
    print('aiohttp_test 异步共花费时间:',end-start)

async def spider_httpx(i,client):
    content = await client.get(url2)
    print('启动{} {}'.format(i,content.status_code))
    
async def httpx_test():
    start = time.time()
    async with httpx.AsyncClient(headers = headers) as client:
        lists = []
        for i in range(10):
            lists.append(asyncio.create_task(spider_httpx(i, client)))
        await asyncio.gather(*lists)
    end=time.time()
    print('aiohttp_test 异步共花费时间:',end-start)

if __name__ =='__main__':
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    # asyncio.run(aiohttp_test())
    asyncio.run(httpx_test())